const express = require('express')
const jwt = require("../jwt/jwt")
const user = require('../db/userSchema')
const mobileuser = require('../db/userSchema_mobile')
const doctor = require('../db/doctorSchema')
var ObjectId = require('mongodb').ObjectID

/* 使用express中的Router对象 */
const router = express.Router()
/**************************************************后台管理账户接口*******************************************************/
// 新增用户
router.post('/addUserMsg', (req, res) => {
	const params = req.body
	user.create(params, (err, result) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '添加成功'
			})
		}
	})
})

// 查询用户列表
router.get('/getUserList', async (req, res) => {
	const { query } = req
	
	let total = 0
	let currentPage = query.currentPage // 当前页
	let pageSize = query.pageSize // 每页显示数量
	let _skip = (currentPage - 1) * pageSize
	
	let userList = user.find({})
	userList.skip(_skip);  // `skip` 跳过指定数量的数据
	userList.limit(pageSize) // 限制条数，每次查多少条
	userList.exec((err, value) => {
		if (err) {
			res.send({
				status: 500,
				msg: '查询错误'
			})
		} else {
			// 查询总数据量
			user.find((err, result) => {
				total = result.length
				res.send({
					status: 200,
					data: {
						code: 0,
						total: total, // 总页数
						userList: [...value]
					},
					msg: '查询当前页成功'
				})
			})
		}
	})
})

// 编辑管理员信息
router.post('/editUserMsg', (req, res) => {
	const params = req.body
	let _id = params._id || ""
	user.updateOne({_id: ObjectId(_id)}, params, (err, docs) => {
		if (err) {
			throw err
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '编辑成功'
			})
		}
	})
})

// 删除用户
router.post('/deleteUser', (req, res) => {
	const params = req.body
	let _id = params._id || ""
	user.deleteOne({_id: ObjectId(_id)}, (err, docs) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '删除成功'
			})
		}
	})
})

/**************************************************医师管理账户接口*******************************************************/
// 新增医师
router.post('/addDoctor', (req, res) => {
	const params = req.body
	doctor.create(params, (err, result) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '添加成功'
			})
		}
	})
})

// 查询医师列表
router.get('/getDoctorList', async (req, res) => {
	const { query } = req
	
	let total = 0
	let currentPage = query.currentPage // 当前页
	let pageSize = query.pageSize // 每页显示数量
	let _skip = (currentPage - 1) * pageSize
	
	let doctorList = doctor.find({})
	doctorList.skip(_skip);  // `skip` 跳过指定数量的数据
	doctorList.limit(pageSize) // 限制条数，每次查多少条
	doctorList.exec((err, value) => {
		if (err) {
			res.send({
				status: 500,
				msg: '查询错误'
			})
		} else {
			// 查询总数据量
			doctor.find((err, result) => {
				total = result.length
				res.send({
					status: 200,
					data: {
						code: 0,
						total: total, // 总页数
						doctorList: [...value]
					},
					msg: '查询当前页成功'
				})
			})
		}
	})
})

// 查询全部医师
router.get('/getAllDoctorList', async (req, res) => {
	let allDoctor = doctor.find({})
	let todayAdd = 0 // 没日新增
	
})

// 编辑医师信息
router.post('/editDoctorMsg', (req, res) => {
	const {id} = req.body
	const params = req.body
	doctor.updateOne({id}, params, (err, docs) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '编辑成功'
			})
		}
	})
})

// 删除医师
router.post('/deleteDoctor', (req, res) => {
	const {id} = req.body
	doctor.deleteOne({id}, (err, docs) => {
		if (err) {
			throw error
		} else {
			res.send({
				status: 200,
				data: {
					code: 0,
				},
				msg: '删除成功'
			})
		}
	})
})

/* 导出router对象 */
module.exports = router